﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WoodManager.Header;
using WoodManager.BO;
using Common;

namespace WoodManager.UI
{
    public partial class ToHopVanSon : Form
    {
        SqlConnection conn;
        DataTable tbVan, tbSon, tbTHVS;
        StructToHopVan stToHopVan = new StructToHopVan("","","","","");
        ToHopVanSonBO thvsBO = new ToHopVanSonBO();
        String ret = CommonHeader.NO_ERROR;
        int iLastRowSelected = -1;
        public ToHopVanSon(SqlConnection adoConn)
        {
            InitializeComponent();
            conn = adoConn;
            //ColumnCombobox
            loadCombobox();
//            getTohopVanInfo();
            setCaption();
        }

        private void loadCombobox()
        {
            tbSon = new DataTable();
            tbVan = new DataTable();
          
            ret = thvsBO.getInfo2Combobox(conn,ref tbVan,ref tbSon);
            //If meet error
            if (ret != CommonHeader.NO_ERROR)
            {
                MessageBox.Show("Error! " + ret);
                return;
            }
            cbbVan.Data = tbVan;
            cbbVan.Columns[1].Display = false;
            cbbVan.Columns[2].Display = false;
            cbbVan.Columns[5].Display = false;
            cbbVan.Columns[8].Display = false;
            cbbVan.Columns[9].Display = false;
            cbbVan.Columns[10].Display = false;
            //cbbVan.ViewColumn = 2;

            cbbSonPhu.Data = tbSon;
            cbbSonPhu.Columns[1].Display = false;
            cbbSonPhu.Columns[2].Display = false;
        }

        private void getTohopVanInfo()
        {
            ret = thvsBO.getToHopVanInfo(conn,ref tbTHVS);
            if (ret != CommonHeader.NO_ERROR)
            {
                MessageBox.Show("Error !" + ret);
                return;
            }
            dtgToHopVanSon.DataSource = tbTHVS;
            if (stToHopVan.sIDTopHopVanSon != "")
            {
                for (int i = 0; i < tbTHVS.Rows.Count; i++)
                {
                    if (stToHopVan.sIDTopHopVanSon == tbTHVS.Rows[i][0].ToString())
                    {
                        dtgToHopVanSon.Rows[i].Selected = true;
                        break;
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            stToHopVan.sIDTopHopVanSon = stToHopVan.sIDVan + stToHopVan.sIDSon;
            stToHopVan.sGhiChu = txtGhiChu.Text;
            if (!validateValue())
                return;
            ret = thvsBO.InsertRecord(conn, stToHopVan);
            if (ret != CommonHeader.NO_ERROR)
            {
                MessageBox.Show("Error !" + ret);
                return;
            }
            getTohopVanInfo();
        }

        private bool validateValue()
        {
            if (stToHopVan.sIDSon == "")
            {
                MessageBox.Show(CommonHeader.ERR_INVALID_VALUE);
                cbbSonPhu.Focus();
                return false;
            }
            if (stToHopVan.sIDVan == "")
            {
                MessageBox.Show(CommonHeader.ERR_INVALID_VALUE);
                cbbVan.Focus();
                return false;
            }
            if (stToHopVan.sIDTopHopVanSon == "")
            {
                MessageBox.Show(CommonHeader.ERR_INVALID_VALUE);
                return false;
            }
            if (stToHopVan.sGhiChu == "")
            {
                stToHopVan.sGhiChu = " ";
            }
            return true;
        }

        private void cbbVan_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbbVan.SelectedIndex > -1)
                {
                    stToHopVan.sIDVan = tbVan.Rows[cbbVan.SelectedIndex]["IDVan"].ToString();
                }
            }
            catch
            {
                //do nothing
            }
        }

        private void cbbSonPhu_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbbSonPhu.SelectedIndex > -1)
                {
                    stToHopVan.sIDSon = tbSon.Rows[cbbSonPhu.SelectedIndex]["IDSon"].ToString();
                }
            }
            catch
            {
                //do nothing
            }
        }

        private void dtgToHopVanSon_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                iLastRowSelected = e.RowIndex;
                cbbVan.Text = tbTHVS.Rows[e.RowIndex]["IDVan"].ToString();
                cbbSonPhu.Text = tbTHVS.Rows[e.RowIndex]["IDSon"].ToString();
                txtGhiChu.Text = tbTHVS.Rows[e.RowIndex]["GhiChu"].ToString();
                stToHopVan.sIDSon = tbTHVS.Rows[e.RowIndex]["IDSon"].ToString();
                stToHopVan.sIDVan = tbTHVS.Rows[e.RowIndex]["IDVan"].ToString();
                stToHopVan.sIDTopHopVanSon = tbTHVS.Rows[e.RowIndex]["IDToHopSon"].ToString();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            stToHopVan.sGhiChu = txtGhiChu.Text;
            if (!validateValue())
                return;
            ret = thvsBO.UpdateRecord(conn, ref stToHopVan);
            if (ret != CommonHeader.NO_ERROR)
            {
                MessageBox.Show("Error !" + ret);
                return;
            }
            getTohopVanInfo();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (stToHopVan.sIDTopHopVanSon == "")
                return;
            ret = thvsBO.DeleteRecord(conn, ref stToHopVan);
            if (ret != CommonHeader.NO_ERROR)
            {
                MessageBox.Show("Error !" + ret);
                return;
            }
            getTohopVanInfo();
        }

        private void setCaption()
        {
            //dtgToHopVanSon.Columns[0].Width = 100;
            //dtgToHopVanSon.Columns[1].Width = 70;
            //dtgToHopVanSon.Columns[2].Width = 200;
            //dtgToHopVanSon.Columns[3].Width = 70;
            //dtgToHopVanSon.Columns[4].Width = 200;
            //dtgToHopVanSon.Columns[5].Width = 100;

        }
    }
}
